<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header
    >
    <!-- 主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EEF"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="activepath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menus" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsobj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+i.path" v-for="i in item.children" :key="i.id" @click="savelink('/'+i.path)">
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{i.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
      this.getMebulist();
      this.activepath=window.sessionStorage.getItem('activepath')
      // this.activepath=window.location.hash.slice(1);
  },
  data(){
      return {
          menus:[],
          iconsobj:{
            '125':'iconfont icon-user',
            '103':'iconfont icon-tijikongjian',
            '102':'iconfont icon-shangpin',
            '145':'iconfont icon-danju',
            '101':'iconfont icon-baobiao',
          },
          //默认不折叠
          iscollapse:false,
         activepath: '',
      }
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有的菜单项
   async getMebulist(){
       const {data:res}= await this.$http.get('menus');
       if(res.meta.status !== 200) return this.$message.err(res.meta.msg)
       this.menus=res.data
    },
    // 点击按钮控制菜单折叠与展开
    toggleCollapse(){
        this.iscollapse=!this.iscollapse
    },
    savelink(activelink){
        window.sessionStorage.setItem('activepath',activelink);
        this.activepath=activelink
    }
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  text-align: center;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
.el-menu{
  border-right: none;
}
.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>